package window

import (
	"fmt"
	"gvms/common"
	"gvms/dowmload"
	"gvms/jvmsDb"
	"gvms/systemRegistry"
	"gvms/util"
	"sort"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
	"github.com/dgraph-io/badger/v4"
	"golang.org/x/sys/windows"
)

func Message(w fyne.Window, message string) {
	dialog.ShowInformation(
		"提示",    // 标题
		message, // 内容
		w,       // 父窗口
	)
}

func SelectJavaHomeDir(db *badger.DB, w fyne.Window, isExit bool, dir *string) {
	/*dialog.ShowFolderOpen(func(uri fyne.ListableURI, err error) {
		if err != nil {
			dialog.ShowError(err, w)
			return
		}

		if uri == nil {
			// 用户取消了选择
			if isExit {
				windows.Exit(1)
			}
			return
		}
		*dir = uri.Path()
		// 显示选择的目录路径
		jvmsDb.InsertDb(db, "JAVA_HOME", uri.Path())
	}, w)*/
	d := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
		if err != nil {
			dialog.ShowError(err, w)
			return
		}

		if uri == nil {
			// 用户取消了选择
			if isExit {
				windows.Exit(1)
			}
			return
		}
		*dir = uri.Path()
		// 显示选择的目录路径
		jvmsDb.InsertDb(db, "GO_HOME", uri.Path())
	}, w)
	d.Resize(fyne.NewSize(450, 200))
	d.Show()
}

func JvmsButton(javaHome *string, db *badger.DB, w fyne.Window, content *fyne.Container, selectVersion *string, jdkVersions map[string]util.JDKVersion) {
	updateButton(javaHome, db, w, content)
	selectVersionButon(selectVersion, jdkVersions, content)
	settingbutton(selectVersion, javaHome, db, w, jdkVersions, content)
	setJsonUrl(content, w, db)
}
func updateButton(javaHome *string, db *badger.DB, w fyne.Window, content *fyne.Container) {
	jdkdir := widget.NewButton("更改go目录", func() {
		SelectJavaHomeDir(db, w, false, javaHome)
	})
	jdkdir.Resize(fyne.NewSize(80, 20))
	jdkdir.Move(fyne.NewPos(10, 15))
	content.Add(jdkdir)
}

func selectVersionButon(selectVersion *string, jdkVersions map[string]util.JDKVersion, content *fyne.Container) {
	keys := make([]string, 0, len(jdkVersions))
	keys = append(keys, "请选择版本")
	for k := range jdkVersions {
		keys = append(keys, k)
	}
	sort.Slice(keys, func(i, j int) bool {
		return keys[i] > keys[j]
	})
	versionSelect := widget.NewSelect(keys, func(s string) {
		if s == "请选择版本" {
			*selectVersion = ""
			return
		}
		*selectVersion = s
	})
	versionSelect.SetSelected("请选择版本")
	versionSelect.Move(fyne.NewPos(180, 42))
	versionSelect.Resize(fyne.NewSize(180, 30))
	label := widget.NewLabel("请选择jdk版本:")
	label.Move(fyne.NewPos(60, 40))
	content.Add(versionSelect)
	content.Add(label)
}

func settingbutton(selectVersion *string, goHome *string, db *badger.DB, w fyne.Window, jdkVersions map[string]util.JDKVersion, content *fyne.Container) {
	downloadJdk := widget.NewButton("设置", func() {
		if *selectVersion == "" {
			Message(w, "请选择go版本")
			return
		}
		progress := widget.NewProgressBarInfinite()
		loadingDialog := dialog.NewCustom("加载中", "取消", progress, w)
		loadingDialog.SetOnClosed(func() {
			// 用户点击取消时的处理
		})
		loadingDialog.Show()
		dowmload.Download(goHome, common.Get[string, util.JDKVersion](jdkVersions, *selectVersion), func(msg string) { Message(w, msg) })
		jvmsDb.InsertDb(db, "GO_HOME", *goHome)
		// 关闭加载对话框
		loadingDialog.Hide()
		systemRegistry.NotifyEnvironmentChanged()
		// 显示完成消息
		dialog.ShowInformation("完成", "设置完成", w)
		fmt.Println("设置完成")
	})
	downloadJdk.Move(fyne.NewPos(300, 100))
	downloadJdk.Resize(fyne.NewSize(80, 20))
	content.Add(downloadJdk)
}

func setJsonUrl(content *fyne.Container, w fyne.Window, db *badger.DB) {
	urlBtn := widget.NewButton("url设置", func() {
		entry := widget.NewEntry()
		entry.SetPlaceHolder("请输入jdk版本维护json文件下载url")
		d := dialog.NewForm("jdk版本维护json下载地址", "确认", "取消", []*widget.FormItem{
			widget.NewFormItem("url地址", entry),
		}, func(b bool) {
			if b {
				util.SetVersionUrl(entry.Text)
				jvmsDb.InsertDb(db, "go_version_json", entry.Text)
			}
		}, w)
		d.Resize(fyne.NewSize(400, 150))
		d.Show()
	})
	urlBtn.Move(fyne.NewPos(10, 160))
	urlBtn.Resize(fyne.NewSize(80, 20))
	content.Add(urlBtn)
}
